﻿using log4net;
using System;
using System.IO;
using System.Reflection;
using TagLib;
using RadioPlaylisterDomain;
using RadioPlaylisterDataAccess;

namespace RadioPlaylisterMediaLoader
{
    class MediaLoaderApi
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        MainForm mainform;
        int fileCount;
        private RadioPlaylisterDataAccess.radioplaylisterEntities context;

        public MediaLoaderApi()
        {
            context = new radioplaylisterEntities();


        }
        public MainForm MainForm
        {
            get { return mainform; }
            set { mainform = value; }
        }

        public void processDirectory(DirectoryInfo rootDirectory)
        {
            try
            {
                processFilesInDirectory(rootDirectory);
                foreach (DirectoryInfo info in rootDirectory.GetDirectories())
                {
                    mainform.OutputWindow.Text += "\n" + info.Name;
                    processDirectory(info);
                }

            }
            catch (UnauthorizedAccessException e) { Log.Debug(e.Message); }
            catch (DirectoryNotFoundException e) { Log.Debug(e.Message); }
            catch (System.Security.SecurityException e) { Log.Debug(e.Message); }
        }

        public void processFilesInDirectory(DirectoryInfo info)
        {
            try
            {
                foreach (FileInfo file in info.GetFiles("*.mp3", SearchOption.TopDirectoryOnly))
                {
                    mainform.OutputWindow.Text += "\n" + file.Name;
                    fileCount++;
                    mainform.FileCountLabel.Text = fileCount.ToString();
                    processFile(file.Name, info.FullName);
                }

            }
            catch (UnauthorizedAccessException e)
            {
                Log.Debug(e.Message);
                //swallow.
                //TODO: figure out how to check for this rather than catch it.
                //Actually, this is the Microsoft recommended approach. 
                //http://msdn.microsoft.com/en-us/library/bb513869.aspx
                //"It is generally too time-consuming to test every folder to determine whether your application has permission to open it."
            }
            catch (System.Security.SecurityException e) { Log.Debug(e.Message); }
        }
        private void  processFile(String filename, String directory)
        {
            Track track = new Track();

            String fullPathToFile = Path.Combine(directory, filename);
            TagLib.File mediaFile = TagLib.File.Create(fullPathToFile);

            track.Artist = mediaFile.Tag.FirstAlbumArtist;
            track.Location = fullPathToFile;
            track.Title = mediaFile.Tag.Title;
            track.TrackNumber = mediaFile.Tag.Track.ToString();
            track.TrackCount = mediaFile.Tag.TrackCount.ToString();

            track.Year =  mediaFile.Tag.Year.ToString();
           // track.Genres =  mediaFile.Tag.Genres;
            track.FirstGenre =  mediaFile.Tag.FirstGenre;
            track.Disc = mediaFile.Tag.Disc.ToString();
            track.DiscCount =  mediaFile.Tag.DiscCount.ToString();
            track.BeatsPerMinute =  mediaFile.Tag.BeatsPerMinute.ToString();
            track.Album = mediaFile.Tag.Album;

            mainform.OutputWindow.Text += "\n " + track.ToString();
            Log.Info(track.ToString());
            saveTrack(track);

        }
        private void saveTrack(Track track)
        {
            context.SaveSong(track);
        }
    }
}
